﻿using System;
namespace Producers.Kis.Xtee.Riik.Ee.Producer.Kis {
    
    
    [global::Xtee.Core.Attributes.XmlTypeAttribute(Name="ConsolidatedData", Namespace="http://producers.kis.xtee.riik.ee/producer/kis", Target=typeof(ConsolidatedData))]
    public class ConsolidatedDataSerializer : global::Xtee.Core.Types.AbstractXteeSerializable {
        
        protected IConsolidatedData clazz;
        
        public ConsolidatedDataSerializer(IConsolidatedData clazz) {
            this.clazz = clazz;
        }
        
        public override void WriteXml(global::Xtee.Core.IXteeXmlWriter Writer) {
            base.WriteXml(Writer);
            Writer.WriteStartElement("FileName");
            if (clazz.FileName!=null) {
                Writer.WriteTypeAttribute("http://producers.kis.xtee.riik.ee/producer/kis", "NonEmptyString");
                Writer.WriteString(clazz.FileName.ToString());
            }
            else {
                throw new global::Xtee.Core.Exceptions.NillableNotAllowedException("Nullable is not allowed for element : FileName");
            }
            Writer.WriteEndElement();
            Writer.WriteStartElement("Organisation");
            if (clazz.Organisation!=null) {
                global::Xtee.Core.SerializationMetadata metaOrganisation = this.GetMetaData(clazz.Organisation);
                Writer.WriteTypeAttribute(metaOrganisation.TypeQualifiedName.Namespace, metaOrganisation.TypeQualifiedName.Name);
                metaOrganisation.NewSerializer(clazz.Organisation).WriteXml(Writer);
            }
            else {
                throw new global::Xtee.Core.Exceptions.NillableNotAllowedException("Nullable is not allowed for element : Organisation");
            }
            Writer.WriteEndElement();
            Writer.WriteStartElement("SenderData");
            if (clazz.SenderData!=null) {
                global::Xtee.Core.SerializationMetadata metaSenderData = this.GetMetaData(clazz.SenderData);
                Writer.WriteTypeAttribute(metaSenderData.TypeQualifiedName.Namespace, metaSenderData.TypeQualifiedName.Name);
                metaSenderData.NewSerializer(clazz.SenderData).WriteXml(Writer);
            }
            else {
                throw new global::Xtee.Core.Exceptions.NillableNotAllowedException("Nullable is not allowed for element : SenderData");
            }
            Writer.WriteEndElement();
            Writer.WriteStartElement("Application");
            if (clazz.Application!=null) {
                Writer.WriteTypeAttribute("http://producers.kis.xtee.riik.ee/producer/kis", "Application");
                Writer.WriteString(clazz.Application.ToString());
            }
            else {
                throw new global::Xtee.Core.Exceptions.NillableNotAllowedException("Nullable is not allowed for element : Application");
            }
            Writer.WriteEndElement();
        }
        
        public override void ReadXml(global::Xtee.Core.IXteeXmlReader Reader) {
            base.ReadXml(Reader);
            if (Reader.LocalName=="FileName") {
                if (Reader.IsElementNil()) {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>ConsolidatedData>FileName");
                }
                clazz.FileName = new global::Producers.Kis.Xtee.Riik.Ee.Producer.Kis.NonEmptyString(Reader.ReadElementString("FileName"));
            }
            else {
                throw new global::Xtee.Core.Exceptions.UnExpectedElementException(">>ConsolidatedData>FileName", Reader.LocalName);
            }
            if (Reader.LocalName=="Organisation") {
                if (Reader.IsElementNil()) {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>ConsolidatedData>Organisation");
                }
                global::Xtee.Core.SerializationMetadata metaOrganisation = this.GetMetaData<global::Producers.Kis.Xtee.Riik.Ee.Producer.Kis.IOrganisation>(Reader, "Organisation", "http://producers.kis.xtee.riik.ee/producer/kis");
                clazz.Organisation = metaOrganisation.NewEntity<global::Producers.Kis.Xtee.Riik.Ee.Producer.Kis.IOrganisation>();
                if ((Reader.IsEmptyElement == false)) {
                    Reader.ReadStartElement("Organisation");
                    metaOrganisation.NewSerializer(clazz.Organisation).ReadXml(Reader);
                    Reader.ReadEndElement();
                }
                else {
                    Reader.Skip();
                }
            }
            else {
                throw new global::Xtee.Core.Exceptions.UnExpectedElementException(">>ConsolidatedData>Organisation", Reader.LocalName);
            }
            if (Reader.LocalName=="SenderData") {
                if (Reader.IsElementNil()) {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>ConsolidatedData>SenderData");
                }
                global::Xtee.Core.SerializationMetadata metaSenderData = this.GetMetaData<global::Producers.Kis.Xtee.Riik.Ee.Producer.Kis.ISenderData>(Reader, "SenderData", "http://producers.kis.xtee.riik.ee/producer/kis");
                clazz.SenderData = metaSenderData.NewEntity<global::Producers.Kis.Xtee.Riik.Ee.Producer.Kis.ISenderData>();
                if ((Reader.IsEmptyElement == false)) {
                    Reader.ReadStartElement("SenderData");
                    metaSenderData.NewSerializer(clazz.SenderData).ReadXml(Reader);
                    Reader.ReadEndElement();
                }
                else {
                    Reader.Skip();
                }
            }
            else {
                throw new global::Xtee.Core.Exceptions.UnExpectedElementException(">>ConsolidatedData>SenderData", Reader.LocalName);
            }
            if (Reader.LocalName=="Application") {
                if (Reader.IsElementNil()) {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>ConsolidatedData>Application");
                }
                clazz.Application = new global::Producers.Kis.Xtee.Riik.Ee.Producer.Kis.Application(Reader.ReadElementString("Application"));
            }
            else {
                throw new global::Xtee.Core.Exceptions.UnExpectedElementException(">>ConsolidatedData>Application", Reader.LocalName);
            }
        }
    }
}
